package com.yksj.healthtalk.ui.home;
import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.app.ListFragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TabHost;
import android.widget.TabHost.TabContentFactory;
import android.widget.TabWidget;
import android.widget.TextView;

import com.yksj.healthtalk.comm.BaseFragmentActivity;
import com.yksj.healthtalk.entity.SalonEntity;
import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.ui.app.AppData;
import com.yksj.healthtalk.ui.app.HTalkApplication;
import com.yksj.healthtalk.ui.salon.SalonNewActivity;
import com.yksj.healthtalk.utils.ToastUtil;
/**
 * 我的沙龙
 * @author Administrator
 *
 */
public class MySalonListActivity extends BaseFragmentActivity implements OnClickListener {
	ViewPager mViewPager;
	TabsAdapter mTabsAdapter;
	TabHost mTabHost;
	AppData mAppData;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.myzone_salon_layout);
		initData();
		initView();
		
		
	}

	public void initView() {
		mViewPager = (ViewPager)findViewById(R.id.view_pager);
		mTabHost = (TabHost)findViewById(android.R.id.tabhost);
		mTabHost.setup();
		mTabsAdapter = new TabsAdapter(this,mViewPager,mTabHost);
		
		Bundle bundle1 = new Bundle();
		bundle1.putString("lable",getString(R.string.last_join));
		bundle1.putString("number",mAppData.getChatList("1").size()+"");
		
		Bundle bundle2 = new Bundle();
		bundle2.putString("lable",getString(R.string.my_attention));
		bundle2.putString("number",mAppData.getChatList("2").size()+"");
		
		Bundle bundle3 = new Bundle();
		bundle3.putString("lable",getString(R.string.my_created));
		bundle3.putString("number",mAppData.getChatList("3").size()+"");
		
		
		mTabsAdapter.addTab(mTabHost.newTabSpec("1"),Fragment.class,bundle1);
		mTabsAdapter.addTab(mTabHost.newTabSpec("2"),Fragment.class,bundle2);
		mTabsAdapter.addTab(mTabHost.newTabSpec("3"),Fragment.class,bundle3);
		
		initTitle();
	}

	public void initData() {
		mAppData = HTalkApplication.getAppData();
	}

	public void initTitle() {
		findViewById(R.id.title_back).setOnClickListener(this);
		TextView mTitleName = (TextView)findViewById(R.id.title_lable);
		mTitleName.setText(getString(R.string.my_salon));
		findViewById(R.id.title_right).setBackgroundResource(R.drawable.icon_create);
		findViewById(R.id.title_right).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent =new Intent(MySalonListActivity.this,SalonNewActivity.class);
				startActivity(intent);
			}
		});
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.title_back:
			this.finish();
			break;
		}
		
	}
	
	protected static class TabsAdapter extends FragmentStatePagerAdapter implements ViewPager.OnPageChangeListener,TabHost.OnTabChangeListener{
		private final Activity mActivity;
		private final ViewPager mViewPager;
		private final TabHost mTabHost;
		
		private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();
		
		static final class TabInfo {
            private final String tag;
            private final Class<?> clss;
            private final Bundle args;
            private final TextView msgTextView;
            TabInfo(String _tag, Class<?> _class, Bundle _args,TextView textView) {
                tag = _tag;
                clss = _class;
                args = _args;
                msgTextView = textView;
            }
        }
		
		public static class TabContentFactoryImpl implements TabContentFactory{
			private final Context mContext;
			public TabContentFactoryImpl(Context context){
				this.mContext = context;
			}
			@Override
			public View createTabContent(String tag) {
				View v = new View(mContext);
				v.setMinimumWidth(0);
				v.setMinimumHeight(0);
				return v;
			}
		}
		
		public TabsAdapter(FragmentActivity fragmentActivity,ViewPager viewPager,TabHost tabHost) {
			super(fragmentActivity.getSupportFragmentManager());
			this.mActivity = fragmentActivity;
			this.mViewPager = viewPager;
			this.mTabHost = tabHost;
			mViewPager.setAdapter(this);
			mViewPager.setOnPageChangeListener(this);
			tabHost.setOnTabChangedListener(this);
		}
		
		public void addTab(TabHost.TabSpec tabSpec,Class<?> _class,Bundle _args){
			View view = LayoutInflater.from(mActivity).inflate(R.layout.salon_tab_tag,null);
			tabSpec.setIndicator(view);
			tabSpec.setContent(new TabContentFactoryImpl(mActivity));
			TextView msgTextView = (TextView)view.findViewById(R.id.tag_mesg_number);
			TextView lableTextView = (TextView)view.findViewById(R.id.tag_lable);
			
			lableTextView.setText(_args.getString("lable"));
			String number = _args.getString("number");
			if(number != null)msgTextView.setText(number);
			
			mTabHost.addTab(tabSpec);
			TabInfo tabInfo = new TabInfo(tabSpec.getTag(), _class, _args,msgTextView);
			mTabs.add(tabInfo);
		}

		@Override
		public Fragment getItem(int arg0) {
			String tag = mTabs.get(arg0).tag;
			Fragment fragment = ArrayListFragment.newInstance(tag);
			return fragment;
		}

		@Override
		public int getCount() {
			return mTabs.size();
		}

		@Override
		public void onPageScrollStateChanged(int arg0) {
			
		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
			
		}

		@Override
		public void onPageSelected(int arg0) {
			TabWidget tabWidget = mTabHost.getTabWidget();
			int  oldFocusability = tabWidget.getDescendantFocusability();
			tabWidget.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
			mTabHost.setCurrentTab(arg0);
			tabWidget.setDescendantFocusability(oldFocusability);
		}

		@Override
		public void onTabChanged(String tabId) {
			int index = mTabHost.getCurrentTab();
			mViewPager.setCurrentItem(index);
		}
	}
	
	@SuppressLint("ValidFragment")
	public static class ArrayListFragment extends ListFragment{
		private String type;//列表类型
		private List<SalonEntity> mList;
		private ArrayListFragment(){
			super();
		}
		
		public ArrayListFragment(String type) {
			super();
			this.type = type;
		}
		
		static ArrayListFragment newInstance(String type){
			ArrayListFragment listFragment = new ArrayListFragment(type);
			return listFragment;
		}
		
		public void onCreate(Bundle savedInstanceState){
			super.onCreate(savedInstanceState);
		}
		
		@Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
			View view  = inflater.inflate(R.layout.list_fragment_layout,null);
			return view;
        }
		
        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
            mList = new ArrayList<SalonEntity>();
            ArrayList<SalonEntity> mArrayList = new ArrayList<SalonEntity>();
            for(int x=0;x<mList.size();x++){
            	mArrayList.add((SalonEntity) mList.get(x));
            }
//            SalonAdapter adapter = new SalonAdapter(getActivity(),mArrayList);
//            setListAdapter(adapter);
        }

        @Override
        public void onListItemClick(ListView l, View v, int position, long id) {
        	ToastUtil.showShort(getActivity(), position+"---------"+l.getCount());
        	SalonEntity mSalonEntity = mList.get(position);
        	//进入指定的salon
        	
        }
	}

}
